package com.zang.concurrent.locks.rwlock;

import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 读写锁测试
 * 写锁可降级为读锁
 * 读锁可重用
 *
 * @author Zhang Qiang
 * @Date 2019/9/17 17:04
 */
public class ReadWriteLockUsage {
    private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
    private final Lock readLock = readWriteLock.readLock();
    private final Lock writeLock = readWriteLock.writeLock();
    private static StringBuilder argString = new StringBuilder();


    public static void main(String[] args) {
        ReadWriteLockUsage readWriteLockUsage = new ReadWriteLockUsage();
        // 循环创建线程获取锁
        for (int i = 0; i < 10; i++) {
            long next = (long) (new Random().nextFloat() * 1000);
            Thread writeThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(next);
                        argString.delete(0,argString.length());
                        argString.append(next);
                        System.out.println(Thread.currentThread().getName() + " -> start" + ", next :" + next + ", argString:" + argString);
                        readWriteLockUsage.wreiter(argString.toString());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            writeThread.setName("T_"+i);
            writeThread.start();

            Thread readThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(next);
                        readWriteLockUsage.reader(argString.toString());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            readThread.start();
        }


    }

    /**
     * 读锁
     *
     * @param argString
     * @return
     */
    public void wreiter(String argString){
        if (writeLock.tryLock()){
            try {
                argString += "_" +ClassLoader.getSystemClassLoader().getParent().toString();
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                writeLock.unlock();
                System.out.println(Thread.currentThread().getName() + " --- writeLock 已释放");
            }
        } else {
            System.out.println(Thread.currentThread().getName() + " 未获取到写锁");
        }
    }

    /**
     * 写锁
     *
     * @param argString
     * @return
     */
    public void reader(String argString){
        readLock.lock();
        try {
            System.out.println("获取到写锁，开始执行： readLock ============== atgString: " + argString);
        } finally {
            readLock.unlock();
        }
    }

}
